package com.jxt.myjxtmanager.ui.model;

import android.content.Context;
import android.os.Handler;
import android.os.Message;

import com.google.gson.Gson;
import com.jxt.myjxtmanager.db.Subject;
import com.jxt.myjxtmanager.db.SubjectModel;
import com.jxt.myjxtmanager.entity.SubjectListBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by qm on 2017/3/7 0007.
 */
public class SubjectMo {

    private String str = "{\n" +
            "    \"code\": 1,\n" +
            "    \"error_code\": 0,\n" +
            "    \"is_msg\": 0,\n" +
            "    \"subjects\": [\n" +
            "        {\n" +
            "            \"id\": 100000001311,\n" +
            "            \"subName\": \"数学\",\n" +
            "            \"subScoreA\": 25,\n" +
            "            \"subScoreB\": 15,\n" +
            "            \"subScoreC\": 10,\n" +
            "            \"subScoreD\": 5\n" +
            "        },\n" +
            "        {\n" +
            "            \"id\": 100000001311,\n" +
            "            \"subName\": \"语文\",\n" +
            "            \"subScoreA\": 22,\n" +
            "            \"subScoreB\": 18,\n" +
            "            \"subScoreC\": 13,\n" +
            "            \"subScoreD\": 2\n" +
            "        },\n" +
            "        {\n" +
            "            \"id\": 100000001311,\n" +
            "            \"subName\": \"英语\",\n" +
            "            \"subScoreA\": 28,\n" +
            "            \"subScoreB\": 10,\n" +
            "            \"subScoreC\": 12,\n" +
            "            \"subScoreD\": 5\n" +
            "        },\n" +
            "        {\n" +
            "            \"id\": 100000001311,\n" +
            "            \"subName\": \"科学\",\n" +
            "            \"subScoreA\": 30,\n" +
            "            \"subScoreB\": 15,\n" +
            "            \"subScoreC\": 5,\n" +
            "            \"subScoreD\": 5\n" +
            "        },\n" +
            "        {\n" +
            "            \"id\": 100000001311,\n" +
            "            \"subName\": \"美术\",\n" +
            "            \"subScoreA\": 28,\n" +
            "            \"subScoreB\": 21,\n" +
            "            \"subScoreC\": 4,\n" +
            "            \"subScoreD\": 2\n" +
            "        },\n" +
            "        {\n" +
            "            \"id\": 100000001311,\n" +
            "            \"subName\": \"体育\",\n" +
            "            \"subScoreA\": 40,\n" +
            "            \"subScoreB\": 10,\n" +
            "            \"subScoreC\": 4,\n" +
            "            \"subScoreD\": 1\n" +
            "        },\n" +
            "        {\n" +
            "            \"id\": 100000001311,\n" +
            "            \"subName\": \"实践课\",\n" +
            "            \"subScoreA\": 30,\n" +
            "            \"subScoreB\": 10,\n" +
            "            \"subScoreC\": 13,\n" +
            "            \"subScoreD\": 2\n" +
            "        },\n" +
            "        {\n" +
            "            \"id\": 100000001311,\n" +
            "            \"subName\": \"作文课\",\n" +
            "            \"subScoreA\": 18,\n" +
            "            \"subScoreB\": 17,\n" +
            "            \"subScoreC\": 12,\n" +
            "            \"subScoreD\": 8\n" +
            "        },\n" +
            "        {\n" +
            "            \"id\": 100000001311,\n" +
            "            \"subName\": \"阅读\",\n" +
            "            \"subScoreA\": 20,\n" +
            "            \"subScoreB\": 15,\n" +
            "            \"subScoreC\": 15,\n" +
            "            \"subScoreD\": 5\n" +
            "        }\n" +
            "        \n" +
            "    ],\n" +
            "    \"msg\": null,\n" +
            "    \"success\": true,\n" +
            "    \"failed\": false,\n" +
            "    \"error\": false\n" +
            "}";


    /**
     * 总数据
     */
    private List<Subject> queryList;
    private final Context context;
    private OnFindAllDataListener onFindAllDevicesListener;

    private Gson mGson;

    public SubjectMo(Context context) {
        this.context = context;
        queryList = new ArrayList<>();
        mGson = new Gson();
    }

    public void setOnFindAllDevicesListener(OnFindAllDataListener onFindAllDevicesListener) {
        this.onFindAllDevicesListener = onFindAllDevicesListener;
    }

    public Subject getQueryList(String subjectName, int position) {
        Subject subjects = SubjectModel.getInstance().getMessageByName(subjectName);
        if (subjects != null) {
            return subjects;
        }
        return queryList.get(position);
    }

    public void setQueryList(List<Subject> queryList) {
        this.queryList = queryList;
    }

    public void RequestData() {
        loadData();
    }


    private void loadData() {
        //模拟网络加载
        Message message = new Message();
        message.arg1 = 2;
        loadDataHandler.sendMessageDelayed(message, 1000);
    }

    private Handler loadDataHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {

            SubjectListBean mResult = mGson.fromJson(str, SubjectListBean.class);
            queryList = mResult.getSubjects();
            SubjectModel.getInstance().insertOrReplaceAll(queryList);
            onFindAllDevicesListener.onFindSuccess();
        }
    };


    /**
     * 请求监听
     */
    public interface OnFindAllDataListener {
        void onFindSuccess();

        void onFailure();

        void onError();
    }

}

